package com.common.manager.abstracts;

import java.util.List;

import com.common.dao.interfaces.IDao;
import com.common.manager.interfaces.IManager;
import com.common.security.exceptions.DatabaseException;
import com.common.security.exceptions.ManagerException;

public abstract class AbstractManager<T> implements IManager<T> {
	
	public List<T> getAll() throws ManagerException {
		try {
			List<T> list = getDao().getAll();
			return list; 
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	public void save(T item) throws ManagerException {
		try {
			getDao().save(item);
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	public void update(T item) throws ManagerException {
		try {
			getDao().update(item);
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	public T getById(Long id) throws ManagerException {
		try {
			T item = getDao().getById(id);
			return item; 
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	public void delete(T item) throws ManagerException {
		try {
			getDao().delete(item);
		} catch (DatabaseException e) {
			throw new ManagerException(ManagerException.DATABASE_EXCEPTION);
		}
	}
	
	protected abstract IDao<T> getDao();
}